﻿var Common = {
    ValidMobile: function (mobile) {
        return mobile.length == 11 && /1[3|4|5|8][0-9]\d{8}/.test(mobile);
    },

    ValidIdNumber: function (idNumber) {
        return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/.test(idNumber);
    },

    // 正整数
    ValidNumber: function (num) {
        return /^\d+$/.test(num);
    },

    // 小数点
    ValidFloat: function (num) {
        return /^\d+\.\d+$/.test(num);
    },

    // 时间：HH:mm
    ValidTime: function (num) {
        return /^([0-1]\d|2[0-3]):[0-5]\d$/.test(num);
    },
    AngleConvert: function (num) {
        return num.replace(/：/g, ":");
    },

    Format: function (format, args) {
        for (var i = 0; i < args.length; i++)
            format = format.replace(RegExp("\\{" + i.toString() + "\\}", "g"), args[i]);

        return format;
    },
    GetTime: function () {

        var dt = new Date();

        var month = dt.getMonth() + 1;
        var day = dt.getDate();
        var hours = dt.getHours();
        var minutes = dt.getMinutes();

        if (month < 10) month = "0" + month;
        if (day < 10) day = "0" + day;
        if (hours < 10) hours = "0" + hours;
        if (minutes < 10) minutes = "0" + minutes;

        var time = Common.Format("{0}-{1}-{2} {3}:{4}", [dt.getFullYear(), month, day, hours, minutes]);
        return time;
    },

    Trim: function (str) {
        for (var i = 0; i < str.length && str.charAt(i) == " "; i++);

        for (var j = str.length; j > 0 && str.charAt(j - 1) == " "; j--);

        if (i > j) return "";

        return str.substring(i, j);
    },

    GetParam: function (paramName) {
        var tmp = location.search;

        if (tmp.length > 0) {
            tmp = decodeURI(tmp);

            var arr = tmp.substring(1).split("&");

            paramName += "=";

            for (var i = 0; i < arr.length; i++) {
                if (arr[i].indexOf(paramName) == 0)
                    return arr[i].substring(paramName.length);
            }
        }

        return undefined;
    },
    Show: function ( title, content) {
        $.messager.show({
            title: title,
            msg: content,
            timeout: 3000,
            showType: 'slide'
        });
    },
    ShowAlert:function(title, msg, icon, fn){
        $.messager.alert(title,msg,icon,fn);
    },
    ShowConfirm:function(title, msg, fn){
        $.messager.confirm(title, msg,fn);
    },
    ShowProcess:function(title,msg,text){
        $.messager.progress(title,msg,text);
    },
    ShowProcess:function(){
        $.messager.progress(title,msg,text);
    },
    CloseProcess:function(){
        $.messager.progress('close');
    }
}

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length >>> 0;
        var from = Number(arguments[1]) || 0;
        from = (from < 0)
            ? Math.ceil(from)
            : Math.floor(from);
        if (from < 0)
            from += len;
        for (; from < len; from++) {
            if (from in this &&
                this[from] === elt)
                return from;
        }
        return -1;
    };
}

//公共调用方法
function Merger(gridName, CellName) {
    //得到显示到界面的id集合
    var mya = $("#" + gridName + "").getDataIDs();
    //当前显示多少条
    var length = mya.length;
    for (var i = 0; i < length; i++) {
        //从上到下获取一条信息
        var before = $("#" + gridName + "").jqGrid('getRowData', mya[i]);
        //定义合并行数
        var rowSpanTaxCount = 1;
        for (j = i + 1; j <= length; j++) {
            //和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
            var end = $("#" + gridName + "").jqGrid('getRowData', mya[j]);
            if (before[CellName] == end[CellName]) {
                rowSpanTaxCount++;
                $("#" + gridName + "").setCell(mya[j], CellName, '', {
                    display : 'none'
                });
            } else {
                rowSpanTaxCount = 1;
                break;
            }
            $("#" + CellName + mya[i]).attr("rowspan", rowSpanTaxCount);
        }
    }
}